# Use this data source to get the ID of a available image
data "huaweicloud_images_image" "default" {
  visibility = "public"
  name        = "Ubuntu 18.04 server 64bit"
  most_recent = true
}

## Use this data source to get the available Compute Flavors.
data "huaweicloud_compute_flavors" "web" {
  availability_zone = "cn-north-4a"
  performance_type  = "computingv3"
  cpu_core_count    = 2
  memory_size       = 4
}


# Create a VPC 
resource "huaweicloud_vpc" "vpc" {
  count      = 1
  cidr       = "172.16.0.0/16"
  name       = "${var.template_name}-vpc"
}

#Create a VPC subnet 
resource "huaweicloud_vpc_subnet" "subnet" {
  vpc_id      = huaweicloud_vpc.vpc[0].id
  name        = "${var.template_name}-subnet"
  cidr        = "172.16.10.0/24"
  gateway_ip  = "172.16.10.1"
  primary_dns = "100.125.1.250"
}

# Create a Security Group
resource "huaweicloud_networking_secgroup" "default" {
  name   ="${var.template_name}-secgroup"
}

# Create a Security Group Rule 1
resource "huaweicloud_networking_secgroup_rule" "allow_ping" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

# Create a Security Group Rule 2
resource "huaweicloud_networking_secgroup_rule" "allow_all" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  ports             = "80"
#  port_range_min    = 1
#  port_range_max    = 65535
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

# Create a Security Group Rule 3
resource "huaweicloud_networking_secgroup_rule" "allow_port22" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  ports             = "22"
#  port_range_min    = 1
#  port_range_max    = 65535
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

# Create ECS
resource "huaweicloud_compute_instance" "web" {
  depends_on = [huaweicloud_vpc_subnet.subnet]
  count = 2
  name              = "${var.template_name}-esc${count.index}"
  image_id          = data.huaweicloud_images_image.default.id
  flavor_id         = data.huaweicloud_compute_flavors.web.ids[0]
  security_groups   = ["${var.template_name}-secgroup"]
  system_disk_type = "SSD"
  system_disk_size = 40
  charging_mode    = "postPaid"
  availability_zone = data.huaweicloud_availability_zones.myaz.names[0]
  admin_pass        = var.password
#  user_data = "#!/bin/bash\napt install nginx -y"
  network {
    uuid = huaweicloud_vpc_subnet.subnet.id
  }
}

data "template_file" "user_data" {
  template = file("./user_data.sh")
}

resource "local_file" "save_inventory" {
  content  = "${data.template_file.user_data.rendered}"
  filename = "./user1.sh"
}

resource "null_resource" "provision_1" {
  depends_on = [huaweicloud_compute_eip_associate.associated,huaweicloud_compute_eip_associate.associated_1]

  provisioner "file" {
    connection {
    type     = "ssh"
    user     = "root"
    password = var.password
    host        = huaweicloud_vpc_eip.myeip[1].address
    }
    source = "./user1.sh"
    destination = "/tmp/user1.sh"
  }
  provisioner "remote-exec" {
    connection {
    type     = "ssh"
    user     = "root"
    password = var.password
    host        = huaweicloud_vpc_eip.myeip[1].address
    }

    inline = [
      "sudo sh /tmp/user1.sh"
    ]
  }
}

resource "null_resource" "provision_2" {
  depends_on = [huaweicloud_compute_eip_associate.associated,huaweicloud_compute_eip_associate.associated_1]

  provisioner "file" {
    connection {
    type     = "ssh"
    user     = "root"
    password = var.password
    host        = huaweicloud_vpc_eip.myeip[2].address
    }
    source = "./user1.sh"
    destination = "/tmp/user1.sh"
  }
  provisioner "remote-exec" {
    connection {
    type     = "ssh"
    user     = "root"
    password = var.password
    host        = huaweicloud_vpc_eip.myeip[2].address
    }

    inline = [
      "sudo sh /tmp/user1.sh"
    ]
  }
}


resource "huaweicloud_compute_eip_associate" "associated_1" {
  public_ip   = huaweicloud_vpc_eip.myeip[1].address
  instance_id = huaweicloud_compute_instance.web[0].id
}

resource "huaweicloud_compute_eip_associate" "associated" {
  public_ip   = huaweicloud_vpc_eip.myeip[2].address
  instance_id = huaweicloud_compute_instance.web[1].id
}


# Create a elb loadbalancer
resource "huaweicloud_lb_loadbalancer" "internet" {
  depends_on = [huaweicloud_vpc_subnet.subnet]
  vip_subnet_id = huaweicloud_vpc_subnet.subnet.subnet_id
  name          = "${var.template_name}-elb"
}

#Create a eip
resource "huaweicloud_vpc_eip" "myeip" {
  publicip {
    type = "5_bgp"
  }
  bandwidth {
    name        = "${var.template_name}-bandwidth"
    size        = 10
    share_type  = "PER"
    charge_mode = "traffic"
  }
  count = 3
}

# bind eip to elb
resource "huaweicloud_networking_eip_associate" "eip_1" {
  depends_on = [huaweicloud_lb_loadbalancer.internet,huaweicloud_vpc_eip.myeip]
  public_ip = huaweicloud_vpc_eip.myeip[0].address
  port_id   = huaweicloud_lb_loadbalancer.internet.vip_port_id
#  count = 1
}

# Create ELB listener 2
resource "huaweicloud_lb_listener" "listener_2" {
  protocol        = "TCP"
  protocol_port   = 80
  loadbalancer_id = huaweicloud_lb_loadbalancer.internet.id
}

#Create ELB pool resource 2
resource "huaweicloud_lb_pool" "group_2" {
  name        = "${var.template_name}-group2"
  protocol    = "TCP"
  lb_method   = "ROUND_ROBIN"
  listener_id = huaweicloud_lb_listener.listener_2.id
}

resource "huaweicloud_lb_member" "member_1" {
  depends_on    = [huaweicloud_compute_instance.web]
  count = 2
  address       = huaweicloud_compute_instance.web[count.index].access_ip_v4
  protocol_port = 80
  weight        = 1
  pool_id       = huaweicloud_lb_pool.group_2.id
  subnet_id     = huaweicloud_vpc_subnet.subnet.subnet_id
}

data "huaweicloud_availability_zones" "myaz" {}

resource "huaweicloud_waf_domain" "domain_1" {
  domain           = var.domain
#  certificate_id   = huaweicloud_waf_certificate.certificate_1.id
#  certificate_name = huaweicloud_waf_certificate.certificate_1.name
  proxy            = true

  server {
    client_protocol = "HTTP"
    server_protocol = "HTTP"
    address         = huaweicloud_vpc_eip.myeip[0].address
    port            = "80"
  }
}

